import spDialog from './index.vue'
import type { DialogOptions } from '@/components/SpDialog/type'

const dialogStore = ref<Array<DialogOptions>>([])

/** 打开弹框 */
const addDialog = (options: DialogOptions) => {
  const open = () => {
    let op = Object.assign(options, {
      open: true,
      destroyOnClose: true,
      maskClosable: false
    })
    // 是否显示底部
    op.showFooter = op.showFooter === undefined ? true : options.showFooter
    // 自定义底部
    if (op.footerRenderer) op.showFooter = false
    if (!op.showFooter && !op.footerRenderer) op.footer = ''
    // 是否全屏
    if (options.fullScreen) {
      op.width = '100%'
      op.wrapClassName = 'full-modal'
    }
    dialogStore.value.push(op)
  }
  open()
}

/** 关闭弹框 */
const closeDialog = (options: DialogOptions, index: number) => {
  dialogStore.value[index].open = false
}

/** 关闭所有弹框 */
const closeAllDialog = () => {
  dialogStore.value = []
}

const SpDialog = defineComponent({
  name: 'SpDialog',
  render: () => {
    return h(spDialog)
  }
})

export { SpDialog, dialogStore, addDialog, closeDialog, closeAllDialog }
